import DTO.DataDTO;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import DTO.TestDTO;
import DTO.UserDTO;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import org.json.JSONObject;
import org.apache.http.HttpStatus;

@WebServlet(urlPatterns = {"/login/dgut"})
public class LoginServlet extends HttpServlet{
    protected void doPost(HttpServletRequest req,
                         HttpServletResponse resp) throws IOException {
        Jsonb jsonb = JsonbBuilder.create();
        DataDTO dataDTO=new DataDTO(req.getParameter("token"),"javaee","b3b52e43ccfd",  req.getLocalAddr());
        String jsonData = jsonb.toJson(dataDTO);
        resp.setContentType("text/html;charset=utf-8");
       try( CloseableHttpClient httpclient = HttpClients.createDefault()) {
           final HttpPost httpPost = new HttpPost("https://cas.dgut.edu.cn/ssoapi/v2/checkToken");
           StringEntity entity = new StringEntity(jsonData, "UTF-8");
           entity.setContentType("application/json");
           entity.setContentEncoding("UTF-8");
           httpPost.setEntity(entity);
           try(CloseableHttpResponse resp1 = httpclient.execute(httpPost)) {
               if(resp1.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                   String result = EntityUtils.toString(resp1.getEntity());// 返回json格式：
                   JSONObject jsonObject = new JSONObject(result);
                   TestDTO testDTO=new TestDTO(jsonObject.getInt("error"),jsonObject.getString("message"),jsonObject.getString("openid"),jsonObject.getString("access_token"));
                   String jsonTest = jsonb.toJson(testDTO);
                   if (jsonObject.getString("message").equals("success")) {
                       final HttpPost httpPost2 = new HttpPost("https://cas.dgut.edu.cn/oauth/getUserInfo");
                       StringEntity entity2 = new StringEntity(jsonTest, "UTF-8");
                       entity2.setContentType("application/json");
                       entity2.setContentEncoding("UTF-8");
                       httpPost2.setEntity(entity2);
                       try(CloseableHttpResponse resp2 = httpclient.execute(httpPost2)) {
                           if(resp2.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                           String result2 = EntityUtils.toString(resp2.getEntity());
                           JSONObject jsonObject2 = new JSONObject(result2);
                           //建立user
                            UserDTO userDTO=new UserDTO( jsonObject2.getString("username"),jsonObject2.getString("name"),
                                    jsonObject2.getString("group"),jsonObject2.getString("openid"),jsonObject2.getString("wx_openid"));
                            req.getSession().setAttribute("userDTO",userDTO);
                           //在jsp页面输入此行
                               resp.sendRedirect("/index.jsp");
                       }
                       }
                   }
               }
           }
       }
    }
    protected void doGet(HttpServletRequest req,
                          HttpServletResponse resp) throws IOException {
        doPost(req,resp);
    }
}
